package com.iptv.grape.jss;

import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentTransaction;

import com.iptv.grape.R;
import com.iptv.grape.beans.WebBean;
import com.iptv.grape.jss.impls.ImplFactory;
import com.iptv.grape.jss.impls.ProCall;
import com.iptv.grape.jss.impls.listener.PlayEventListener;
import com.iptv.grape.jss.impls.listener.VideoCallBack;
import com.iptv.grape.jss.impls.listener.WebCallBack;
import com.iptv.grape.ui.BaseFrgmt;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.video.VideoSize;
import com.iptv.grape.video.XMediaPlayer;

public class ThirdFrgmt extends BaseFrgmt implements WebCallBack.WebBackImpl, VideoCallBack.VideoBackImpl, ProCall {
    private CallWebView mWebview;
    private SurfaceView surfaceView;
    private XMediaPlayer mediaPlayer = null;
    private ProgressBar progressBar;
    // 暂停相关的控件
    private RelativeLayout pauseLayout;
    private PlayEventListener playerEventListener;
    private VideoCallBack videoCallBack;


    private WebFrgmt2 webFrgmt;
    @Override
    protected int layoutId() {
        return R.layout.act_third_party;
    }


    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        ImplFactory.istc().addProCall(this);
        mWebview = view.findViewById(R.id.cwvView);
        surfaceView = view.findViewById(R.id.surfaceView);
        progressBar = view.findViewById(R.id.progressBar);
        pauseLayout = view.findViewById(R.id.pause_layout);

        playerEventListener = new PlayEventListener();
        mediaPlayer = new XMediaPlayer(surfaceView, progressBar);
        mediaPlayer.setOnPlayerListener(playerEventListener);
        mWebview.initHardwareAccelerate(mActivity);
        videoCallBack = new VideoCallBack(this);
        mWebview.addJavascriptInterface(new WebCallBack(this), "bfWebCallBack");
        mWebview.addJavascriptInterface(videoCallBack, "bsVideoCallBack");
        callBack.playUrlVideo("http://222.85.81.15:6060/HKVOD/32/3768e4977e0c20180508182959428378?virtualDomain=HKVOD.vod_hpd.zte.com",20,50,180,100,false);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //取消注册事件
        //EventBus.getDefault().unregister(this);
        if(null==mWebview)
            return;
        ViewParent parent = mWebview.getParent();
        if (parent != null) {
            ((ViewGroup) parent).removeView(mWebview);
        }
        mWebview.stopLoading();
        mWebview.getSettings().setJavaScriptEnabled(false);
        mWebview.clearHistory();
        mWebview.clearView();
        mWebview.removeAllViews();
        mWebview.destroy();
        mWebview = null;
    }


    @Override
    public void recvWebImpl(WebBean bean) {
        String tag = bean.tag;
        switch (tag){
            case WebCallBack.TAG_WEB_openBST:
                String contentID = bean.param.contentID;
                break;

            case WebCallBack.TAG_WEB_quitActivity:

                break;
            case WebCallBack.TAG_WEB_jumpWebUrl:
                String openType = bean.param.openType;
                if(WebCallBack.TAG_OEPN_URL_TYPE_WEB.equals(openType)){

                }
                break;
        }
    }

    @Override
    public void playUrlVideoImpl(String url) {
        if (mediaPlayer.isPlaying()) {
            // 如过正在播放点播
            mediaPlayer.stop();
        }
        mediaPlayer.playVideo(mActivity,url);
    }

    @Override
    public void playUrlVideoImpl(String url, int left, int top, int width, int height, boolean isLoop) {
        VideoSize videoSize = new VideoSize(width,height,left,top);
        mediaPlayer.play(mActivity, url,videoSize,isLoop);
        remove();

    }

    @Override
    public void setPositionImpl(int left, int top, int width, int height) {
        VideoSize videoSize = new VideoSize(width,height,left,top);
        mediaPlayer.setPosition(videoSize);
        remove();
    }

    @Override
    public void playFullScreenImpl() {
        mediaPlayer.fullScreen();
        ImplFactory.istc().setInitPro(mediaPlayer.getDuration(),mediaPlayer.getCurrentPosition());
        show();

    }

    @Override
    public void pauseImpl() {
        mediaPlayer.pause();
    }

    @Override
    public void playImpl() {
        mediaPlayer.start();
    }

    @Override
    public void seekImpl(int sec) {
        mediaPlayer.seekTo(sec);
    }

    @Override
    public void closeVideoImpl() {
        mediaPlayer.stop();
    }

    @Override
    public boolean isPlayingImpl() {
        return mediaPlayer.isPlaying();
    }

    @Override
    public int getTotalTimeImpl() {
        return mediaPlayer.getDuration();
    }

    @Override
    public int getCurrentTimeImpl() {
        return mediaPlayer.getCurrentPosition();
    }

    @Override
    public void setSartPro() {

    }

    @Override
    public void setPro(int pro) {
        mediaPlayer.seekTo(pro);
    }



    private void show() {

        if (webFrgmt == null) {
            AppLog.getCurLog().e("show===>1");
            webFrgmt = WebFrgmt2.newInstance();
//            playerEventListener.addProListener(webFrgmt);
            FragmentTransaction transaction = this.getChildFragmentManager().beginTransaction();
            transaction.add(R.id.rel_hk, webFrgmt, "webFrgmt");
            transaction.commit();
        } else {
            AppLog.getCurLog().e("show===>2");
            this.getChildFragmentManager().beginTransaction().show(webFrgmt)
                    .commit();
        }
    }
    private void remove(){

        if(null!=webFrgmt){
            AppLog.getCurLog().e("show===>3");
            FragmentTransaction transaction = this.getChildFragmentManager().beginTransaction();
            transaction.hide(webFrgmt);
            transaction.commit();
        }
    }

    public static ThirdFrgmt newInstance() {
        ThirdFrgmt frag = new ThirdFrgmt();
        Bundle bundle = new Bundle();
        //bundle.putString("dateTimeString", dateTimeString);
        frag.setArguments(bundle);   //设置参数
        return frag;
    }

    private VideoCallBack callBack;
    public void setVideoCallBack(VideoCallBack callBack){
            this.callBack = callBack;
    }
}
